package com.mo.modular.system.service.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import sun.misc.BASE64Decoder;

import com.mo.config.properties.MoProperties;
import com.mo.system.service.ISaveFileService;

@Service
public class SaveFileServiceImpl implements ISaveFileService {
	 @Resource
	    private MoProperties MoProperties;
	@Override
	public String saveBase64File( String fileName, String base64) {

        String fileSavePath = MoProperties.getFileUploadPath();
		File savedir = new File(fileSavePath);
		try {
			BASE64Decoder decoder = new BASE64Decoder();
			byte[] bytes = decoder.decodeBuffer(base64);
			for (int i = 0; i < bytes.length; ++i) {
				if (bytes[i] < 0) {// 调整异常数据
					bytes[i] += 256;
				}
			}
			if (!savedir.exists()){
				savedir.mkdirs();// 如果目录不存在就创建
			}
			File file = new File(savedir, fileName);
			FileOutputStream fileoutstream = new FileOutputStream(file);
			fileoutstream.write(bytes);
			fileoutstream.flush();
			fileoutstream.close();
		} catch (IOException e) {
			return "error";
		}
		return fileName;
	}

	@Override
	public boolean delFile(String deldir) {
		if (deldir != null) {
			File dirFile = new File(deldir);
			dirFile.delete();
		}
		return true;
	}

}
